<?php

class mHoliday extends CI_Model {
    
        ########################################################################
        ####### DATA HOLIDAY ##############
        ########################################################################
        function save($edit="")
        {
                $dblokal  = $this->load->database("default", TRUE);
		$tanggal = $this->input->post("tanggal");
		$keterangan = $this->input->post("keterangan");
		
		$data = array("tanggal"=>str_replace("-","",$tanggal),
			      "keterangan"=>$keterangan,
			      "created_by"=>$this->session->userdata('username'),
                              "created_on"=>date("Y-m-d H:i:s"));
		
                if($edit<>""){ 
			$dblokal->where(array("id"=>$edit));
			$dblokal->update("t_mtr_holiday",$data);
		}else{
			$dblokal->insert("t_mtr_holiday",$data);
		}
                $this->updateWorkingDay("sfa",substr($tanggal,4,2));
        }
        
        function updateWorkingDay($jenis,$bulan){
		$dblokal  = $this->load->database("default", TRUE);
		/*
		1. Cek jumlah hari pada bulan tahun ybs
		2. Cek jumlah hari libur pada bulan & tahun
		3. Cek hari libur pada hari apa?
		*/
                
                $tahun=date("Y");
		$num = cal_days_in_month(CAL_GREGORIAN,$bulan,$tahun);
                $holiday = count($this->getHoliday($bulan,$tahun));
                $weekend = 0;
                $days = $this->mservices->getValueFromTable('t_mtr_weekly_holiday',array('type'=>$jenis));
                $i=1;
                foreach($days as $r){
                        for($k=1;$k<=7;$k++){
                                if($r->{'d'.$k}==1){
                                        for($n=1;$n<=$num;$n++){
                                                if(date("N", mktime(0,0,0,$bulan,$n,$tahun)) == $k) $weekend++;
                                        }
                                }
                        }
                        $i++;
                }
                $work = $num - $holiday - $weekend;
                $jml = $this->mglobal->showdata("lookup_value","t_mtr_lookup",array('lookup_key'=>'workday_'.$jenis.'_'.$tahun.$bulan),"dblokal");
                if($jml==""){
                        #data belum ada, insert
                        $dblokal->insert("t_mtr_lookup",array('lookup_key'=>'workday_'.$jenis.'_'.$tahun.$bulan,'lookup_value'=>$work,'lookup_desc'=>$work));
                }else{
                        #dta sudah ada, update
                        $dblokal->query("UPDATE t_mtr_lookup SET lookup_value = ". $work .", lookup_desc = ". $work ." WHERE lookup_key =  'workday_".$jenis."_".$tahun.$bulan."'");
                }
	}
        
        function getHoliday($bulan,$tahun){
                $dblokal = $this->load->database("default", TRUE);
		
                $dblokal->select('*');
                $dblokal->from("t_mtr_holiday");
                $dblokal->like('tanggal', $tahun.$bulan, 'after'); 
                $query = $dblokal->get();
                
                if( $query->num_rows() > 0 ) {
                        return $query->result();
                } else {
                        return array();
                }
        }
        
        function get($id = "")
        {    
                $dblokal  = $this->load->database("default", TRUE);
                if($id<>""){
                        $dblokal->where(array("id"=>$id));
                }
                $query = $dblokal->get("t_mtr_holiday");
                if( $query->num_rows() > 0 ) {
                        return $query->result();
                } else {
                        return array();
                }
	}
        
        function saveWeeklyHoliday($jenis,$hari,$nilai){
                $dblokal = $this->load->database("default", TRUE);
                $dblokal->where('type',$jenis);
                $dblokal->update('t_mtr_weekly_holiday',array('d'.$hari=>$nilai));
                
                $this->updateWorkingDay("ds",date("m"));
        }
        
        
        #########################
        #########################
        
        public function getById( $id ) {
                #$id = intval( $id );
                
                $query = $this->db->where( 'id', $id )->limit( 1 )->get( 't_mtr_holiday' );
                
                if( $query->num_rows() > 0 ) {
                    return $query->row();
                } else {
                    return array();
                }
        }
        
        public function getAll() {
                //get all records from users table
                $query = $this->db->get( 't_mtr_holiday' );
                
                if( $query->num_rows() > 0 ) {
                    return $query->result();
                } else {
                    return array();
                }
        } //end getAll
        
        public function update() {
                $data = array(
                    'tanggal' => $this->input->post( 'tanggal', true ),
                    'keterangan' => $this->input->post( 'keterangan', true )
                );
                $this->db->update( 't_mtr_holiday', $data, array( 'id' => $this->input->post( 'id', true ) ) );
        }
        
        public function delete( $id ) {
                /*
                * Any non-digit character will be excluded after passing $id
                * from intval function. This is done for security reason.
                */
                $id = intval( $id );
                $this->db->delete( 't_mtr_holiday', array( 'id' => $id ) );
        } //end delete
    
} //end class